define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order_statement/index' + location.search,
                    // add_url: 'order/order_statement/add',
                    edit_url: 'order/order_statement/edit',
                    // del_url: 'order/order_statement/del',
                    // multi_url: 'order/order_statement/multi',
                    // import_url: 'order/order_statement/import',
                    table: 'order_statement',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'statement_type', title: __('Statement_type'), searchList: {"order":__('Statement_type order'),"aftersales":__('Statement_type aftersales')}, formatter: Table.api.formatter.normal},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'order_id', title: __('Order_id')},
                        // {field: 'refund_order_id', title: __('Refund_order_id')},
                        {field: 'order.order_no', title: __('Order.order_no'), operate: 'LIKE', visible: false},
                        {field: 'refund.refund_no', title: __('Refund.refund_no'), operate: 'LIKE', visible: false},
                        {field: 'order_no', title: __('销售单号/售后单号'), operate: false, formatter: function (value,row,index) {
                                switch (row.statement_type) {
                                    case 'order':
                                        return row.order.order_no;
                                    case 'aftersales':
                                        return row.refund.refund_no;
                                }
                            }},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: '账单详情',
                                    title: '账单详情',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-sticky-note-o',
                                    extend: 'data-area=\'["90%", "90%"]\'',
                                    url: function (res) {
                                        return 'order/order_statement/detail/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        return true;
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: '上传凭证信息',
                                    title: '上传凭证信息',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    icon: 'fa fa-pencil',
                                    extend: 'data-toggle="tooltip"',
                                }
                            ],
                            formatter: Table.api.formatter.operate
                            // formatter:function (value, row, index) {
                            //     var that = $.extend({}, this);
                            //     $(table).data("operate-edit", 'aaa'); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                            //     that.table = table;
                            //     return Table.api.formatter.operate.call(that, value, row, index);
                            // }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
